import com.safezh.entity.UserTabColumn;
import com.safezh.handler.CustomBeanListHandler;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.db.DbUtil;
import com.xiaoleilu.hutool.db.Entity;
import com.xiaoleilu.hutool.db.SqlRunner;
import com.xiaoleilu.hutool.db.ds.DSFactory;
import com.xiaoleilu.hutool.db.meta.TableType;
import com.xiaoleilu.hutool.db.sql.Condition;
import com.xiaoleilu.hutool.extra.template.VelocityUtil;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.velocity.VelocityContext;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @ClassName:Test
 * @Description:TODO(用一句话描述该文件做什么)
 * @date:2018年01月03日
 * @since JDK 1.7
 */
public class Test {
    public static Log log = LogFactory.get();

    public static void main(String[] args) {
        DataSource dataSource = DSFactory.get();
        SqlRunner sqlRunner = SqlRunner.create(dataSource);

        List<String> tables = DbUtil.getTables(dataSource);
        try {
            final List<Entity> entities = sqlRunner.findLike("user_tables", "TABLE_NAME", "EMS_SUC_NORMALTASK", Condition.LikeType.EndWith);
            List<String> tablenames = new ArrayList<String>();
            for (Entity entity : entities) {
                log.info(String.valueOf(entity.get("TABLE_NAME")));
                tablenames.add(String.valueOf(entity.get("TABLE_NAME")));
            }
            Collections.sort(tablenames);
            log.info(Test.class.getResource("template").getPath().substring(1));
            VelocityUtil.init(Test.class.getResource("template").getPath().substring(1), "UTF-8");
            VelocityContext velocityContext = new VelocityContext();
            velocityContext.put("tablenames", tablenames);
            VelocityUtil.toFile("table.vm", velocityContext, "template/table.txt");

            for (String tableName : tablenames) {
                String className = StrUtil.upperFirst(StrUtil.toCamelCase(tableName));
                String date = DateUtil.formatDateTime(new Date());
                String module = tableName.split("_")[0].toLowerCase();

                List<UserTabColumn> userTabColumns = sqlRunner.query("select DISTINCT utc.TABLE_NAME,UTC.column_Name,UCC.comments,utc.data_type from user_tab_columns utc \n" +
                        "LEFT JOIN user_col_comments ucc on (ucc.column_Name = UTC.column_Name and ucc.TABLE_NAME = UTC.TABLE_NAME)\n" +
                        "where utc.Table_Name='" + tableName + "'", new CustomBeanListHandler<UserTabColumn>(UserTabColumn.class));

                for (UserTabColumn userTabColumn :
                        userTabColumns) {

                    if (StrUtil.containsIgnoreCase(userTabColumn.getDataType(), "CHAR")) {
                        userTabColumn.setColumnType("String");
                        userTabColumn.setJdbcType("VARCHAR");
                    }
                    if (userTabColumn.getDataType().equalsIgnoreCase("DATE")) {
                        userTabColumn.setColumnType("Date");
                        userTabColumn.setJdbcType("TIMESTAMP");
                    }
                    if (userTabColumn.getDataType().equalsIgnoreCase("NUMBER")
                            || userTabColumn.getDataType().equalsIgnoreCase("DECIMAL")) {
                        userTabColumn.setColumnType("BigDecimal");
                        userTabColumn.setJdbcType("DECIMAL");
                    }
                    userTabColumn.setColumnName(StrUtil.toCamelCase(userTabColumn.getColumnName()).toLowerCase());
                    userTabColumn.setNameOfGet(StrUtil.genGetter(userTabColumn.getColumnName()));
                    userTabColumn.setNameOfSet(StrUtil.genSetter(userTabColumn.getColumnName()));

                }


                final List<Table> fieldNames = sqlRunner.query("SELECT\n" +
                        "\tcolumn_Name\n" +
                        "FROM\n" +
                        "\tuser_cons_columns\n" +
                        "WHERE\n" +
                        "\tconstraint_name = (\n" +
                        "\t\tSELECT\n" +
                        "\t\t\tconstraint_name\n" +
                        "\t\tFROM\n" +
                        "\t\t\tuser_constraints\n" +
                        "\t\tWHERE\n" +
                        "\t\t\ttable_name = '" + tableName.toUpperCase() + "'\n" +
                        "\t\tAND constraint_type = 'P'\n" +
                        "\t)", new CustomBeanListHandler<Table>(Table.class));
                for (Table table : fieldNames) {
                    table.setColumnName(table.getColumnName().toLowerCase());
                }
                velocityContext.put("fieldNames", fieldNames);
                velocityContext.put("columnFK", fieldNames.get(0).getColumnName().toLowerCase());
                velocityContext.put("columnFKGet", StrUtil.genGetter(fieldNames.get(0).getColumnName()));
                velocityContext.put("className", className);
                velocityContext.put("classNameFL", StrUtil.lowerFirst(className));
                velocityContext.put("classNameLower", className.toLowerCase());
                velocityContext.put("tableName", tableName);
                velocityContext.put("date", date);
                velocityContext.put("userTabColumns", userTabColumns);
                velocityContext.put("module", module);


                VelocityUtil.toFile("entity.vm", velocityContext, "com/zwsafety/module/" + module + "/entity/" + className + ".java");
                VelocityUtil.toFile("dao.vm", velocityContext, "com/zwsafety/module/" + module + "/dao/" + className + "Mapper.java");
                VelocityUtil.toFile("mapper.vm", velocityContext, "com/zwsafety/module/" + module + "/mapper/oracle/" + className + "Mapper-oracle.xml");
                VelocityUtil.toFile("service.vm", velocityContext, "com/zwsafety/module/" + module + "/service/" + className + "Service.java");
                VelocityUtil.toFile("serviceimpl.vm", velocityContext, "com/zwsafety/module/" + module + "/service/impl/" + className + "ServiceImpl.java");
                VelocityUtil.toFile("controller.vm", velocityContext, "com/zwsafety/module/" + module + "/controller/" + className + "Controller.java");
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

